#!/usr/bin/env bash
set -eu

shopt -s nullglob

###############################################################################
#                                                                             #
# This script will run every time your environment is built.                  #
# A dotfiles repo may be help in managing your tooling and personalization.   #
#                                                                             #
# More info: https://enterprise.coder.com/docs/dotfiles                       #
#                                                                             #
# /!\ This file will be automatically recreated if it is deleted! /!\         #
#                                                                             #
###############################################################################

##### BEGIN: Dotfiles clone behavior.

function configure_dotfiles () {
  # `DOTFILES_REPO` will be automatically provided during your environment's build.

  if [[ ! -v DOTFILES_REPO ]]; then
    printf "This Coder account has not configured Dotfiles or a personalize script.
    \nAll files and applications outside of your home directory will be lost if the environment is rebuilt or turned off due to inactivity.
    \nWe recommend configuring a Dotfiles repo or personalize script to ensure your preferences are applied every time your environment turns on.
    \nCoder Enterprise Documentation on Dotfiles: https://enterprise.coder.com/docs/dotfiles
    \n                               on personalize: https://enterprise.coder.com/docs/the-~personalize-script\n"

    return 0
  fi

  echo "Dotfiles repo configured at $DOTFILES_REPO"

  if [[ ! -x "$(command -v git)" ]]; then
    echo "Git not found, skipping dotfiles configuration..."

    return 0
  fi

  DOTFILES_CLONE_PATH=$HOME/dotfiles
  DOTFILES_INSTALL_SCRIPT_PATH=$DOTFILES_CLONE_PATH/install.sh
  LAST_DOTFILES_CLONE_URI_PATH=$HOME/.last_dotfiles_clone_uri

  # Cloning of the dotfiles repo occurs in a non-interactive script,
  # thus unverified hosts must be permitted without user confirmation.
  # Temporarily disables remote host key validation to avoid permanently saving an unverified key.
  NO_SSH_VALIDATION="ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no"

  if [[ -f $LAST_DOTFILES_CLONE_URI_PATH ]]; then
    LAST_URI=$(< "$LAST_DOTFILES_CLONE_URI_PATH")

    if [[ ("$DOTFILES_REPO" != "$LAST_URI") && -d $DOTFILES_CLONE_PATH ]]; then
      DATE_SUFFIX=$(date +"%s")
      BACKUP_DOTFILES_CLONE_PATH="${DOTFILES_CLONE_PATH}_backup_${DATE_SUFFIX}"

      echo "Dotfiles repo was has changed from $LAST_URI"
      echo "Moving $DOTFILES_CLONE_PATH to $BACKUP_DOTFILES_CLONE_PATH"

      mv "$DOTFILES_CLONE_PATH" "$BACKUP_DOTFILES_CLONE_PATH"
    fi
  fi

  if [[ ! -d $DOTFILES_CLONE_PATH ]]; then
    echo "Cloning repo $DOTFILES_REPO into $DOTFILES_CLONE_PATH"

    GIT_SSH_COMMAND=$NO_SSH_VALIDATION git clone "$DOTFILES_REPO" "$DOTFILES_CLONE_PATH"
    echo "$DOTFILES_REPO" > "$LAST_DOTFILES_CLONE_URI_PATH"
  fi

  cd "$DOTFILES_CLONE_PATH"
  GIT_SSH_COMMAND=$NO_SSH_VALIDATION git pull --ff-only

  echo
  if [[ -x "$DOTFILES_INSTALL_SCRIPT_PATH" ]]; then
    echo "Executing installation script found in $DOTFILES_INSTALL_SCRIPT_PATH"
    "$DOTFILES_INSTALL_SCRIPT_PATH"
  else
    echo "Installation script $DOTFILES_CLONE_PATH not found or not executable"
    echo "Symlinking all files and directories starting with '.' into $HOME"

    for dotfile in "$DOTFILES_CLONE_PATH/".*; do
      # Skip `..` and '.'
      [[ $dotfile =~ \.{1,2}$ ]] && continue
      # Skip Git related
      [[ $dotfile =~ \.git$ ]] && continue
      [[ $dotfile =~ \.gitignore$ ]] && continue
      [[ $dotfile =~ \.gitattributes$ ]] && continue

      echo "Symlinking $dotfile"
      ln -sf "$dotfile" "$HOME"
    done
  fi

  cd "$HOME"
}

configure_dotfiles

##### END: Dotfiles clone behavior.

###############################################################################
#                                                                             #
# Add shell commands below if this environment has a unique personalization.  #
#                                                                             #
###############################################################################

echo "build project / install dependencies for repo"

REPO_1_PATH=$HOME/coder-react
NPM_INSTALL_COMMAND="npm install"
NPM_START_COMMAND="npm start"
YARN_INSTALL_COMMAND="yarn"
YARN_START_COMMAND="yarn start"

if [ -d $REPO_1_PATH ]; then
    echo "Directory for coder-react Node.js/React repo exists."
    cd $REPO_1_PATH
    echo "do build / install dependencies with NPM"
    $NPM_INSTALL_COMMAND
    #echo "start React app"
    #$NPM_START_COMMAND
fi

echo "install fish shell"

FISH_BINARY=/usr/bin/fish
FISH_PATH=/usr/bin

if [ ! -f $FISH_BINARY ] ; then
    sudo apt-get update
    sudo apt-get install -y fish
    echo "installing fish in $FISH_PATH"
else
    echo "fish already installed"
fi

#echo "changing shell"
#chsh -s /usr/bin/fish $USER